Skip to content

Conversation

@brlee19
Copy link
Contributor

@brlee19 brlee19 commented Sep 19, 2025

Adds unitScale to read and write paths.

I tested this by creating a test.tf in examples that uses unit_scale:

resource "datadog_dashboard" "test_unit_scale" {
  title       = "blee Unit Scale Bug Test final!"
  description = "Testing unit_scale bug - should fail on master, work on fix branch"
  layout_type = "free"

  widget {
    query_value_definition {
      title       = "Test Widget with unit_scale"
      title_align = "left"
      title_size  = "16"
      autoscale   = true
      precision   = 2

      request {
        formula {
          formula_expression = "query1 * 100"

          number_format {
            unit {
              canonical {
                unit_name = "percent"
              }
            }
            unit_scale {
              unit_name = "percent"
            }
          }
        }

        query {
          metric_query {
            aggregator  = "sum"
            data_source = "metrics"
            name        = "query1"
            query       = "sum:system.cpu.user{*}"
          }
        }
      }
    }

    widget_layout {
      x      = 0
      y      = 0
      width  = 8
      height = 15
    }
  }
}

On the master branch, terraform apply successfully creates a dashboard but omits unit_scale:

image

If I check out this branch and try again, this time it actually works:

image

If I check out master again, this time terraform plan fails, reproducing the original error about unit_name:

datadog_dashboard.test_unit_scale: Refreshing state... [id=vay-6ue-tsi]

Planning failed. Terraform encountered an error while generating this plan.

╷
│ Error: widget.0.query_value_definition.0.request.0.formula.0.number_format.0.unit_scale.0.unit_name: '' expected type 'string', got unconvertible type '[]map[string]interface {}', value: '[map[unit_name:0x14000b56210]]'
│
│   with datadog_dashboard.test_unit_scale,
│   on test.tf line 15, in resource "datadog_dashboard" "test_unit_scale":
│   15: resource "datadog_dashboard" "test_unit_scale" {
│
╵
FAIL

unitScale := map[string]interface{}{}
unitScale["unit_name"] = []map[string]interface{}{{"unit_name": v.UnitName}}
unitScale := map[string]interface{}{"unit_name": v.UnitName}
m["unit_scale"] = []map[string]interface{}{unitScale}
Copy link
Contributor Author

@brlee19 brlee19 Sep 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The schema for this is:

"unit_scale": &schema.Schema{
			Description: "",
			Type:        schema.TypeList,
			MinItems:    0,
			MaxItems:    1,
			Optional:    true,
			Elem: &schema.Resource{
				Schema: map[string]*schema.Schema{
					"unit_name": &schema.Schema{
						Description: "",
						Type:        schema.TypeString,
						Required:    true,
					},
				},
			},
		},

so m["unit_scale"] is supposed to contain a list of map[string]string if I am reading this right, not another map containing "unit_name" as a key

Copy link
Contributor Author

@brlee19 brlee19 Sep 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The old code appears to be the cause of the error per the CI output:

level=fatal msg="Execution failed" error="error: error generating plan: exit status 1\n\nError: widget.1.group_definition.0.widget.0.query_value_definition.0.request.0.formula.0.number_format.0.unit_scale.0.unit_name: '' expected type 'string', got unconvertible type '[]map[string]interface {}', value: '[map[unit_name:0xc0009716e0]]'\n\n with datadog_dashboard.maze_api_dashboard,\n on dashboard.tf line 1, in resource \"datadog_dashboard\" \"maze_api_dashboard\":\n 1: resource \"datadog_dashboard\" \"maze_api_dashboard\" {\n\n code: INVALID_ARGUMENT origin: terraform-invocation, action: plan, cause: USER, resource: \n"

aka it is saying that the first element of unit_scale had a []map[string]interface {}' at its unit_name key and not just a string

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure why the old code was working until Wednesday though this probably has something to do with the new release that day.

@brlee19 brlee19 marked this pull request as ready for review September 24, 2025 14:45
@brlee19 brlee19 requested review from a team as code owners September 24, 2025 14:45
}

func buildNumberFormatFormulaSchema(terraformStyle map[string]interface{}) *datadogV1.WidgetNumberFormat {
func buildDatadogNumberFormatFormulaSchema(terraformStyle map[string]interface{}) *datadogV1.WidgetNumberFormat {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The buildDatadog functions are on the write path (turning HCL to Datadog schema), renamed this for clarity.

}
}
}
if v, ok := terraformStyle["unit_scale"].([]interface{}); ok && len(v) > 0 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't able to actually create a dashboard with this field with unitScale until adding this too. Uses the same schema as below.

@brlee19 brlee19 changed the title Fix type error in building of unitScale [Dashboards] Fix type error in unitScale Oct 22, 2025
Copy link

@bonnierhee bonnierhee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this makes sense to me - is there any risk of this affecting existing terraforms? I would assume not since the comment makes it sound like this was working until recently?

@brlee19
Copy link
Contributor Author

brlee19 commented Oct 22, 2025

this makes sense to me - is there any risk of this affecting existing terraforms? I would assume not since the comment makes it sound like this was working until recently?

I actually don't think this field was ever working with terraform and that this field only made its way into the dashboard because it was added via in the UI (it was not exclusively terraform-managed).

This seems like a relatively simple change though and I don't think it would cause any issues, especially since it is basically just fixing broken code we have already (that implements a field that has been in our public API spec since February) and also adding the fix to the read path, not just the write.

@brlee19
Copy link
Contributor Author

brlee19 commented Oct 22, 2025

/merge

@dd-devflow-routing-codex
Copy link

dd-devflow-routing-codex bot commented Oct 22, 2025

View all feedbacks in Devflow UI.

2025-10-22 19:12:19 UTC ℹ️ Start processing command /merge


2025-10-22 19:12:24 UTC ℹ️ MergeQueue: pull request added to the queue

The expected merge time in master is approximately 27m (p90).


2025-10-22 19:37:51 UTC ℹ️ MergeQueue: This merge request was merged

@dd-mergequeue dd-mergequeue bot merged commit cd96436 into master Oct 22, 2025
15 of 20 checks passed
@dd-mergequeue dd-mergequeue bot deleted the blee/fix-buildTerraformNumberFormatFormulaSchema branch October 22, 2025 19:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants